package com.lmqb.controller;

import com.lmqb.bean.SysUser;
import com.lmqb.constant.MsgError;
import com.lmqb.constant.MsgSuccess;
import com.lmqb.entry.LeftMenu;
import com.lmqb.entry.MenuTitle;
import com.lmqb.entry.ValidateCode;
import com.lmqb.responces.ResultData;
import com.lmqb.service.JiaoYanService;
import com.lmqb.service.SysUserService;
import com.lmqb.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class LoginController extends BaseController{

	@Autowired
	private JiaoYanService jiaoYanService;
	
	@Autowired
	private SysUserService sysUserService;
	
	//跳转到登录页面
	@RequestMapping(value="login.html",method=RequestMethod.GET)
	public String toLogin(HttpServletRequest request,HttpSession session,HttpServletResponse response){
		return "login";
	}
	
	//生成图形验证码
	@RequestMapping(value="/validateCode")  
	public String validateCode(HttpServletRequest request,HttpServletResponse response) throws Exception{
		// 设置响应的类型格式为图片格式
		response.setContentType("image/jpeg");
		//禁止图像缓存。
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		HttpSession session = request.getSession();
		ValidateCode vCode = new ValidateCode(120,40,5,200);
		session.setAttribute("code", vCode.getCode());
		vCode.write(response.getOutputStream());
		return null;
	}
	
	//登录操作
	@RequestMapping(value="/login",method=RequestMethod.POST)
	public String login(HttpServletRequest request,
			HttpServletResponse response,HttpSession session,
			String username,String password){
		System.out.println("审核通过" + username);
		ResultData<SysUser> resultData =new ResultData<SysUser>();
		try{
			resultData=jiaoYanService.adminLogin(username,password);
			if(resultData.getStatus()!=0){
				request.setAttribute("username", username);
				request.setAttribute("password", password);
				request.setAttribute("errMessage", resultData.getMsg());
				return "login";
			}
//			code=code.toUpperCase();`
//			String codeS=((String)session.getAttribute("code")).toUpperCase();
//			if(!code.equals(codeS)){
//				resultData.setError(MsgError.ErrCode);
//				return resultData;
//			}
			//登录操作，判断用户名是否等于admin，
			//等于的话，直接比较用户名密码是否相等。
			//否则比较用户名和加密后的密码是否相等。
			resultData=jiaoYanService.selectByUserName(username);
			if(resultData.getStatus()==1){
				request.setAttribute("username", username);
				request.setAttribute("password", password);
				request.setAttribute("errMessage", resultData.getMsg());
				return "login";
			}
			String pwdMd5=StrUtil.Get_MD5(password);
			if(!pwdMd5.equals(resultData.getData().getUserPwd())){
				resultData.setError(MsgError.ErrPwd);
				request.setAttribute("username", username);
				request.setAttribute("password", password);
				request.setAttribute("errMessage", resultData.getMsg());
				return "login";
			}else{
				resultData.getData().setLastLoginTimeD(StrUtil.intToTime(resultData.getData().getLastLoginTime()));
				session.setAttribute(memberAdmin, resultData.getData());
				//修改管理员表的信息，添加管理员登录记录
				String ip=request.getRemoteAddr();
				
				sysUserService.updateLogin(ip,resultData.getData());
				//获取用户的角色、权限，放到session中，用来生成左侧菜单
				List<LeftMenu> leftMenu=sysUserService.selectRoleMenu(resultData.getData());
				session.setAttribute(LeftMenuF, leftMenu);
				resultData.setChengGong(resultData.getData());
			}
		}catch(Exception e){
			e.printStackTrace();
			resultData.setError(MsgError.SysErr);
			request.setAttribute("username", username);
			request.setAttribute("password", password);
			request.setAttribute("errMessage", MsgError.SysErr);
		}
		return "index";
	}
	
	//跳转到index页面
	@RequestMapping(value="index.html",method=RequestMethod.GET)
	public String toIndex(HttpSession session,HttpServletRequest request,
			HttpServletResponse response){
		SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
		if(sysUser==null){
			return "login";
		}
		MenuTitle menuTitle=new MenuTitle();
		menuTitle.setParent("个人中心");
		menuTitle.setMenu("个人信息");
		session.setAttribute(menuTitleF, menuTitle);
		return "index";
	}
	
	//进入修改密码页面
	@RequestMapping(value="toEditPwd.html",method=RequestMethod.GET)
	public String toEditPwd(HttpSession session,HttpServletRequest request,
			HttpServletResponse response){
		SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
		if(sysUser==null){
			return "login";
		}
		MenuTitle menuTitle=new MenuTitle();
		menuTitle.setParent("个人中心");
		menuTitle.setMenu("修改密码");
		session.setAttribute(menuTitleF, menuTitle);
		return "editPwd";
	}
	
	//修改密码操作
	@RequestMapping(value="/editPwd",method=RequestMethod.POST)
	@ResponseBody
	public ResultData<String> editPwd(HttpServletRequest request,
			HttpServletResponse response,HttpSession session,
			String pwd,String pwd1,String pwd2){
		ResultData<String> resultData =new ResultData<String>();
		try{
			SysUser sysuser=(SysUser)session.getAttribute(memberAdmin);
			if(sysuser==null){
				resultData.setError(MsgError.NotLogin);
				return resultData;
			}
			resultData=jiaoYanService.editPwd(pwd,pwd1,pwd2,sysuser.getUserId());
			if(resultData.getStatus()==1){
				return resultData;
			}
			SysUser admin=new SysUser();
			admin.setUserId(sysuser.getUserId());
			admin.setUserPwd(StrUtil.Get_MD5(pwd1));
			sysUserService.updateAdmin(admin);
			resultData.setSuccess(MsgSuccess.ChengGong, MsgSuccess.ChengGong);
		}catch(Exception e){
			e.printStackTrace();
			resultData.setError(MsgError.SysErr);
		}
		return resultData;
	}
	
	//退出登录
	@RequestMapping (value="logout",method=RequestMethod.GET)
	public String logout(HttpSession session,HttpServletRequest request){
		SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
		session.removeAttribute(memberAdmin);
		return "redirect:login.html";
	}
	
}
